package com.agileboot.medicine.controller;

import cn.hutool.core.lang.Assert;
import com.agileboot.common.core.dto.ResponseDTO;
import com.agileboot.domain.feedback.db.IUserFeedbackService;
import com.agileboot.domain.feedback.db.UserFeedback;
import com.agileboot.domain.feedback.dto.UserFeedbackReq;
import com.agileboot.infrastructure.user.AuthenticationUtils;
import com.agileboot.infrastructure.user.web.SystemLoginUser;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@RestController
@RequestMapping("/feedback")
public class UserFeedbackController {
    @Autowired
    private IUserFeedbackService userFeedbackService;


    @PostMapping("/save")
    @ApiOperation(value = "提交反馈", notes = "提交反馈")
    public ResponseDTO saveFeedback(@RequestBody UserFeedbackReq userFeedbackReq) {
        //确定需求，哪些参数是不许要有值的
        if (StringUtils.isBlank(userFeedbackReq.getContent())
                || StringUtils.isBlank(String.valueOf(userFeedbackReq.getUserId()))) {
            return ResponseDTO.fail("参数不合法");
        }

        userFeedbackService.addFeedback(userFeedbackReq);
        return ResponseDTO.ok("保存成功");
    }


    @PostMapping("/page")
    @ApiOperation(value = "反馈分页", notes = "反馈分页")
    public ResponseDTO pageFeedback(
            @ApiParam("size") @RequestParam(value = "size", required = false,defaultValue = "15") Integer size,
            @ApiParam("current") @RequestParam(value = "current", required = false,defaultValue = "1") Integer current,
            @ApiParam("status")  @RequestParam(value = "status", required = false,defaultValue = "-1") Integer status) {

        Page paramPage = new Page<>();
        paramPage.setSize(size);
        paramPage.setCurrent(current);
        paramPage.addOrder(OrderItem.desc("create_time"));
        SystemLoginUser user = AuthenticationUtils.getSystemLoginUser();
        //校验参数u
        Assert.isTrue(Objects.nonNull(user),"用户没登陆");
        IPage resPage = userFeedbackService.page(paramPage,new LambdaQueryWrapper<UserFeedback>()
                        .eq(UserFeedback::getUserId, user.getUserId())
                        .eq(status!=-1,UserFeedback::getStatus,status));
        return ResponseDTO.ok(resPage);
    }

    @GetMapping("/detail")
    @ApiOperation(value = "反馈详情", notes = "反馈详情")
    public ResponseDTO detail(
            @ApiParam(value = "反馈id",required = true) @RequestParam("id") Long id,
            @ApiParam(value = "userId",required = true)  @RequestParam("userId") Long userId) {

        Assert.isTrue(StringUtils.isNotBlank(String.valueOf(id)),"参数为空");
        UserFeedback one = userFeedbackService.getById(id);
        if (one == null || !one.getUserId().equals(userId)) {
            return ResponseDTO.fail("参数不合法，请检查");
        }
        return ResponseDTO.ok(one);
    }

}
